package com.huawei.hianalytics.ab.bc.de;

import android.content.Context;
import android.text.TextUtils;
import com.huawei.hianalytics.ab.ab.bc;
import com.huawei.hianalytics.ab.bc.cd.bc.de;
import com.huawei.hianalytics.ab.bc.ij.ab;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONException;

public class ef {
    public static Map<String, List<de>> ab(Context context, String str) {
        if (context == null) {
            return null;
        }
        Map bc = ab.bc(context, str);
        ab(bc);
        return bc(bc);
    }

    public static Map<String, List<de>> ab(Context context, String str, String str2) {
        Map ab;
        Map ab2;
        if ("alltype".equals(str2) || TextUtils.isEmpty(str)) {
            com.huawei.hianalytics.ab.bc.ef.ab.bc("EventManager", "read all event records");
            ab = ab(context, "stat_v2_1");
            ab2 = ab(context, "cached_v2_1");
        } else {
            String ab3 = com.huawei.hianalytics.ab.bc.kl.ef.ab(str, str2);
            ab = bc(context, "stat_v2_1", ab3);
            ab2 = bc(context, "cached_v2_1", ab3);
        }
        return ab(ab, ab2);
    }

    private static Map<String, List<de>> ab(Map<String, List<de>> map, Map<String, List<de>> map2) {
        if (map.size() == 0 && map2.size() == 0) {
            return new HashMap();
        }
        if (map.size() == 0) {
            return map2;
        }
        if (map2.size() == 0) {
            return map;
        }
        Map<String, List<de>> hashMap = new HashMap();
        for (Entry entry : map.entrySet()) {
            String str = (String) entry.getKey();
            List list = (List) entry.getValue();
            list.addAll((List) map2.get(str));
            hashMap.put(str, list);
        }
        return hashMap;
    }

    private static void ab(String str, String str2, Map<String, List<de>> map) {
        List arrayList = new ArrayList();
        try {
            if (!TextUtils.isEmpty(str2)) {
                JSONArray jSONArray = new JSONArray(str2);
                if (jSONArray.length() != 0) {
                    for (int i = 0; i < jSONArray.length(); i++) {
                        de deVar = new de();
                        try {
                            deVar.ab(jSONArray.getJSONObject(i));
                            arrayList.add(deVar);
                        } catch (JSONException e) {
                            com.huawei.hianalytics.ab.bc.ef.ab.cd("EventManager", "JSON Exception happened when create data for report - readDataToRecord");
                        }
                    }
                    map.put(str, arrayList);
                }
            }
        } catch (JSONException e2) {
            com.huawei.hianalytics.ab.bc.ef.ab.cd("EventManager", "When events turn to JSONArray,JSON Exception has happened");
        }
    }

    private static void ab(Map<String, ?> map) {
        Iterator it = map.entrySet().iterator();
        Set ab = com.huawei.hianalytics.ab.bc.kl.ef.ab(bc.ab());
        while (it.hasNext()) {
            if (!ab.contains(((Entry) it.next()).getKey())) {
                it.remove();
            }
        }
    }

    public static Map<String, List<de>> bc(Context context, String str, String str2) {
        String bc = ab.bc(context, str, str2, "");
        Map hashMap = new HashMap();
        ab(str2, bc, hashMap);
        return hashMap;
    }

    private static Map<String, List<de>> bc(Map<String, ?> map) {
        Map hashMap = new HashMap();
        for (Entry entry : map.entrySet()) {
            String str = (String) entry.getKey();
            if (entry.getValue() instanceof String) {
                ab(str, (String) entry.getValue(), hashMap);
            }
        }
        return hashMap;
    }
}
